clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear


	texdoc close 
	cap erase "$dir/Tables/TableA1.tex"
	texdoc init "$dir/Tables/TableA1.tex", force

	tex \begin{table}[tbph]
	tex \caption{Effect of electoral victory on donation type}\label{tab:bal_mis}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome:&  Has non-family donors  & Has family donors & Other races \\
	tex & (1) & (2) & (3) \\ \hline
	tex & & & \\
	
	*Model 1
	foreach x in dat_no_fam dat_fam {
	
		*Summary statistics for the mean
		quietly sum `x' if margin_victory!=.
			local mean_`x' : di %5.3f r(mean)
			local sd_`x' : di %5.3f r(sd) 
		
		*Regressions
		rdrobust `x' margin_victory ,  p(1) vce(cluster muni_code)
		
		*Local's for the table
		local bw_`x' : di %5.2f `e(h_l)'
		local ser_`x' = round(`e(se_tau_rb)',0.001)
		local Neff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local N_`x' = `e(N)'
		local poly_`x' = `e(p)'
		local beta1_`x' : di %5.3f `e(tau_cl)'
		local beta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local ser1_`x' : di %5.3f `e(ci_l_rb)'
			local ser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local em1_`x' = (`beta1_`x''/`mean_`x'')*100 
			local em1_`x' : di %5.2f `em1_`x''
			
		*P-values
		local pval2_`x' : di %5.3f `e(pv_rb)'
		scalar pval2_`x' = e(pv_rb)

	}	
	


	*Continue table
	tex Electoral victory & `beta1_dat_no_fam' & `beta1_dat_fam' & `beta1_b2b' \\
	tex \ \ \ \ Robust p-value & `pval2_dat_no_fam' & `pval2_dat_fam' & `pval2_b2b' \\
	tex \ \ \ \ CI 95\%  & [`ser1_dat_no_fam',`ser2_dat_no_fam'] & [`ser1_dat_fam',`ser2_dat_fam'] & [`ser1_b2b',`ser2_b2b'] \\
	tex & & & \\
	
	tex Observations & `N_dat_no_fam' & `N_dat_fam' & `N_b2b' \\
	tex Bandwidth obs. & `Neff_dat_no_fam' & `Neff_dat_fam' & `Neff_b2b' \\
	tex Mean & `mean_dat_no_fam' & `mean_dat_fam' & `mean_b2b' \\
	tex Bandwidth & `bw_dat_no_fam' & `bw_dat_fam' & `bw_b2b' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. Robust p-values with clustering at the municipality level 95\% robust confidence intervals are computed following \cite{calonico_robust_2014}. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 
